﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//using Modbus.Device;
//using Modbus.Utility;
using MySql.Data.MySqlClient;

namespace TJ_MainNameSpace
{
    public partial class Peizhi2 : Form
    {
        public Peizhi2()
        {
            InitializeComponent();
        }

        //MySqlConnection con = new MySqlConnection(new MySQLConnectionString(TongJi_EMS.Properties.Settings.Default["server"].ToString(), TongJi_EMS.Properties.Settings.Default["database"].ToString(), TongJi_EMS.Properties.Settings.Default["mysqluser"].ToString(), TongJi_EMS.Properties.Settings.Default["mysqlpassword"].ToString(), shujuyuan3.port).AsString);
        MySqlConnection con = new MySqlConnection(TongJi_EMS.Properties.Settings.Default.monitorConnection);

        public static string port = "COM1";
        public static string baudrate = "9600";
        public static string databit = "8";
        public static string parity = "无校验";
        public static string stopbit = "1";

        private void button2_Click(object sender, EventArgs e)
        {

            try
            {

                //Peizhi1 up = new Peizhi1();
                string p1 = Peizhi1.new_name;
                string p2 = Peizhi1.address;
                con.Open();

                string sql;
                //数据库重新设计后的最新更改
                string sql1 = "";
                string sqlCheckarea = "select count(*) from dbarea where area_name = '" + Peizhi1.area_name + "'";
                MySqlCommand checkarea = new MySqlCommand(sqlCheckarea, con);
                int count = Convert.ToInt16(checkarea.ExecuteScalar());
                #region 老代码
                //if (For_shebei.ifalart == false)
                //{

                //    string mysql = "SELECT   slaverid FROM  device where device.name='" + Peizhi1.new_name + "'AND project='" + For_shebei.ifalart + "'";
                //    MySqlCommand Cmd2 = new MySqlCommand(mysql, con);
                //    object count = Cmd2.ExecuteScalar();


                //    if (count == null)



                //        sql = "INSERT INTO device (readtimeout,name,slaverid,COM,BaudRate,DataBits,Parity,StopBits,protocol,project) VALUES('" + Peizhi1.outtime + "','" +

                //     p1 + "','" + p2 + "','" + comboBox1.Text + "','" + comboBox2.Text + "','" + comboBox3.Text + "','" + comboBox4.Text + "','" + comboBox5.Text + "','" + Peizhi1.protocol + "','" + FormStart.Gongcheng_Name + "')";


                //    else
                //        sql = "update device setreadtimeout='" + Peizhi1.outtime + "', name='" + p1 + "',slaverid='" + p2 + "',COM='" + comboBox1.Text + "',BaudRate='" + comboBox2.Text + "',DataBits='" + comboBox3.Text + "',Parity='" + comboBox4.Text + "',StopBits='" + comboBox5.Text + "' where name='" + Peizhi1.new_name + "'AND project='" + FormStart.Gongcheng_Name + "'";
                //}
                //else
                //{
                //    sql = "update device set name='" + p1 + "',slaverid='" + p2 + "',COM='" + comboBox1.Text + "',BaudRate='" + comboBox2.Text + "',DataBits='" + comboBox3.Text + "',Parity='" + comboBox4.Text + "',StopBits='" + comboBox5.Text + "' where name='" + Peizhi1.new_name + "'AND project='" + FormStart.Gongcheng_Name + "'";
                //    For_shebei.ifalart = false;
                //} 
                #endregion

                #region 数据库重新设计后的最新更改
                if (For_shebei.ifalart == false)
                {
                    sql = @"INSERT INTO device (slaver_address,name,area_id,area_name,protocol,COM,BaudRate,DataBits,Parity,StopBits,readtimeout,project) VALUES('"
                        + Peizhi1.address + "','" + Peizhi1.new_name + "',(select id from dbarea where area_name = '" + Peizhi1.area_name + "'),'" + Peizhi1.area_name + "','" + Peizhi1.protocol + "','"
                        + this.comboBox1.Text + "','" + this.comboBox2.Text + "','"
                        + this.comboBox3.Text + "','" + this.comboBox4.Text + "','"
                        + this.comboBox5.Text + "','" + Peizhi1.outtime + "','" + TongJi_EMS._Management.ProjectManager.Instance.CurProjectName + "')";
                    sql1 = @"insert into dbarea (area_name,project) values ('" + Peizhi1.area_name + "','" + TongJi_EMS._Management.ProjectManager.Instance.CurProjectName + "')";
                }
                else
                {
                    sql = @"update device set slaver_address='" + Peizhi1.address + "',name='" + Peizhi1.new_name + "',area_name='" + Peizhi1.area_name + "',area_id = (select id from dbarea where area_name = '" + Peizhi1.area_name
                        + "'),COM='" + this.comboBox1.Text + "',BaudRate='" + this.comboBox2.Text
                        + "',DataBits='" + this.comboBox3.Text + "',Parity='" + this.comboBox4.Text
                        + "',StopBits='" + this.comboBox5.Text + "' where name = '" + Peizhi1.name + "'";
                    For_shebei.ifalart = false;
                }

                if (sql1 != "" && count == 0)
                {
                    MySqlCommand cmd = new MySqlCommand(sql1, con);
                    cmd.ExecuteNonQuery();
                }
                #endregion
                MySqlCommand mInsertCmd2 = new MySqlCommand(sql, con);

                mInsertCmd2.ExecuteNonQuery();

                con.Close();

                //For_shebei jm = new For_shebei();
                Peizhi1.protocol = "";
                Peizhi1.name = "";
                Peizhi1.address = "";


                this.Close();


            }
            catch (Exception ex1)
            {
                MessageBox.Show(ex1.ToString());
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Peizhi1 a = new Peizhi1();
            a.Show();
            this.Close();
        }

        private void peizhi2_Load(object sender, EventArgs e)
        {
            this.comboBox1.SelectedIndex = this.comboBox1.FindString(Peizhi2.port);
            this.comboBox2.SelectedIndex = this.comboBox2.FindString(Peizhi2.baudrate);
            this.comboBox3.SelectedIndex = this.comboBox3.FindString(Peizhi2.databit);
            this.comboBox4.SelectedIndex = this.comboBox4.FindString(Peizhi2.parity);
            this.comboBox5.SelectedIndex = this.comboBox5.FindString(Peizhi2.stopbit);
        }
    }
}